package com.qiqu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiqu.Vo.ArticleVo;

import com.qiqu.Vo.UsersCollectionVo;
import com.qiqu.Vo.UsersFansVo;
import com.qiqu.dao.UsersDao;
import com.qiqu.dao.mapper.ArticleMapperCustom;

import com.qiqu.dao.mapper.UsersFansCustom;
import com.qiqu.entity.Enclosure;
import com.qiqu.entity.UsersCollection;
import com.qiqu.entity.UsersFans;
import com.qiqu.dao.UsersFansDao;
import com.qiqu.service.UsersFansService;
import com.qiqu.utils.JSONResult;
import com.qiqu.utils.PagedResult;
import com.qiqu.utils.TimeAgoUtils;
import org.n3r.idworker.Sid;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("usersFansService")
public class UsersFansServiceImpl implements UsersFansService {
    @Resource
    private UsersFansDao usersFansDao;
    @Resource
    private UsersDao usersDao;
    @Resource
    private Sid sid;
    @Resource
    private UsersFansCustom usersFansCustom;

    @Override
    public boolean Fan(String userId, String fanId) {
        UsersFans usersFans = new UsersFans(sid.nextShort(), userId, fanId,1,new Date());
        // 关注者关注数加一；被关注者粉丝数加一
        int i = usersFansDao.insert(usersFans);
        if (i>0) {
            usersDao.addFollow(userId);
            usersDao.addFans(fanId);
            return true;
        }
        return false;
    }

    @Override
    public boolean cancleFan(String userId, String fanId) {
        int i = usersFansDao.delete(usersFansDao.query(userId, fanId));
        if (i>0) {
            usersDao.subFans(fanId);
            usersDao.subFollow(userId);
            return true;
        }
        return false;
    }

    @Override
    public boolean isFan(String userId, String fanId) {
        Example u = new Example(UsersFans.class);
        Example.Criteria criteria = u.createCriteria();
        criteria.andEqualTo("userId",userId);
        criteria.andEqualTo("fanId",fanId);
        List<UsersFans> usersFans = usersFansDao.selectByExample(u);
        if(usersFans==null || usersFans.size()==0){
            return false;
        }
        return true;
    }

    @Override
    public int updateStatus(String userId) {

        return usersFansDao.updateStatus(userId);
    }

    @Override
    public int queryStatusCount(String userId) {

        return usersFansDao.queryStatusCount(userId).size();
    }

    @Override
    public PagedResult getFans(Integer page, Integer pageSize, String userId) {
        PageHelper.startPage(page,pageSize);
        List<UsersFansVo> usersFansVos= usersFansCustom.queryList(userId);
        for(UsersFansVo usersFansVo:usersFansVos){
            usersFansVo.setTimeDesc(TimeAgoUtils.format(usersFansVo.getCreateTime()));
        }
        PageInfo<UsersFansVo> pageList=new PageInfo<>(usersFansVos);
        PagedResult pagedResult =new PagedResult();
        pagedResult.setPage(page);
        pagedResult.setTotal(pageList.getPages());
        pagedResult.setRows(usersFansVos);
        pagedResult.setRecords(pageList.getTotal());
        return pagedResult;
    }


}